package com.service.impl;
import com.entity.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.DeptMapper;
import com.mapper.EmpMapper;
import com.mapper.RoleMapper;
import com.mapper.StoresMapper;
import com.service.IEmpService;
import com.until.ConstantUtil;
import com.vo.Page;
import org.apache.shiro.crypto.hash.Sha512Hash;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class EmpServiceImpl implements IEmpService {
    @Resource
    private EmpMapper empMapper;
    @Resource
    private DeptMapper deptMapper;
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private StoresMapper storesMapper;
    @Resource
    private HttpSession session;
    @Override
    public PageInfo<Emp> listByParam(Page<Emp> page) {
        Emp empInfo = (Emp) session.getAttribute("empInfo");
        Integer storesId = empInfo.getStoresId();
        PageHelper.startPage(page.getPageNum(),page.getPageSize());
        Emp data = page.getData();
        data.setStoresId(storesId);
        return new PageInfo<Emp>(empMapper.listByParam(data));
    }

    @Override
    public Emp getByUsername(Emp emp) {
        emp.setStatus(ConstantUtil.EmpStatus.ENABLED);
        return empMapper.getByUsername(emp);
    }
    @Override
    public Emp getByEmpId(Integer empId) {
        return empMapper.getByEmpId(empId);
    }

    @Override
    public int insert(Emp emp) {
        //获取随机盐值
        String randomSalt= UUID.randomUUID().toString();
        //设置盐值属性
        emp.setSalt(randomSalt);
        //获取原始密码
        String originalPassword=emp.getPassword();
        //计算加密密码
        Sha512Hash sha512Hash=new Sha512Hash(originalPassword,randomSalt,
                ConstantUtil.CredentialMatcher.HASHITERATIONS);
        //设值密码属性
        emp.setPassword(sha512Hash.toString());

        return empMapper.insert(emp);
    }

    @Override
    public int delete(Integer empId) {
        return empMapper.delete(empId);
    }

    @Override
    public int update(Emp emp) {
        return empMapper.update(emp);
    }

    @Override
    public PageInfo<Emp> listByStatus(Page<Emp> page) {
        PageHelper.startPage(page.getPageNum(),page.getPageSize());
        Emp emp=new Emp();
        emp.setStatus(ConstantUtil.EmpStatus.DISABLED);
        return new PageInfo<Emp>(empMapper.listByParam(emp));
    }

    @Override
    public int updateStatus(Emp emp) {
        emp.setStatus(ConstantUtil.EmpStatus.ENABLED);
        return empMapper.update(emp);
    }

    @Override
    public int insertFlat(EmpFlatCheck empFlatCheck) {
        return empMapper.insertFlat(empFlatCheck);
    }

    @Override
    public List<EmpFlatCheck> listEmpFlatCheck() {
        EmpFlatCheck empFlatCheck=new EmpFlatCheck();
        empFlatCheck.setFlatStatus(ConstantUtil.FlatStatus.FLAT);
        return empMapper.listEmpFlatCheck(empFlatCheck);
    }

    @Override
    public List<EmpFlatCheck> listEmpFlatHistory() {
        EmpFlatCheck empFlatCheck1=new EmpFlatCheck();
        empFlatCheck1.setFlatStatus(ConstantUtil.FlatStatus.ENABLED);
        EmpFlatCheck empFlatCheck2=new EmpFlatCheck();
        empFlatCheck2.setFlatStatus(ConstantUtil.FlatStatus.DISABLED);
        // 定义数组，放平调审核状态为2 和3 的信息
        List<EmpFlatCheck> empFlatChecks=new ArrayList<>();
        empFlatChecks.addAll(empMapper.listEmpFlatCheck(empFlatCheck1));
        empFlatChecks.addAll(empMapper.listEmpFlatCheck(empFlatCheck2));
        return empFlatChecks;
    }

    @Override
    public int updateFire(Emp emp) {
        emp.setStatus(ConstantUtil.EmpStatus.DISABLED);
        return empMapper.update(emp);
    }

    @Override
    @Transactional
    public int updateFlat2(EmpFlatCheck empFlatCheck) {
        empFlatCheck.setFlatStatus(ConstantUtil.FlatStatus.ENABLED);
        int num1=empMapper.updateFlat(empFlatCheck);
        // 通过该员工的姓名找到员工共的信息，并修改
        Integer empId=empFlatCheck.getEmpId();
        System.out.println(empFlatCheck);
        //System.out.println("empId------------------");
        //System.out.println(empId);
        String flatDeptName=empFlatCheck.getFlatDeptName();
        Dept dept=new Dept();
        dept.setDeptName(flatDeptName);
        Integer deptId=deptMapper.listByParam(dept).get(0).getDeptId();
        String flatRoleName=empFlatCheck.getFlatRoleName();
        Role role=new Role();
        role.setRoleName(flatRoleName);
        Integer roleId=roleMapper.listByParam(role).get(0).getRoleId();
        String flatStoresName=empFlatCheck.getFlatStoresName();
        Stores stores=new Stores();
        stores.setStoresName(flatStoresName);
        Integer storesId=storesMapper.listStoresName(stores).get(0).getStoresId();
        System.out.println("-------------------------------------------");
        System.out.println(storesId);
        Emp ep=empMapper.getByEmpId(empId);
        ep.setDeptId(deptId);
        ep.setRoleId(roleId);
        ep.setStoresId(storesId);
        ep.setEmpId(empId);
        //System.out.println("--------------------------------------------------");
        System.out.println(ep);
        int num2=empMapper.update(ep);
        if(num1>0 && num2>0){
            return 1;
        }
        return 0;
    }

    @Override
    public int updateFlat3(EmpFlatCheck empFlatCheck) {
        empFlatCheck.setFlatStatus(ConstantUtil.FlatStatus.DISABLED);
        return empMapper.updateFlat(empFlatCheck);
    }

    @Override
    public int deleteFlat(Integer empFlatId) {
        return empMapper.deleteFlat(empFlatId);
    }

    @Override
    public int updateFreeze(Emp emp) {
        emp.setStatus(ConstantUtil.EmpStatus.FREEZE);
        return empMapper.update(emp);
    }

    @Override
    public int updateUnfreeze(Emp emp) {
        emp.setStatus(ConstantUtil.EmpStatus.ENABLED);
        return empMapper.update(emp);
    }
}
